package com.xz.stu.server.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.stu.server.entity.Classroom;
import com.xz.stu.server.service.ClassroomService;
import com.xz.stu.server.mapper.ClassroomMapper;
import com.xz.stu.server.vo.classroom.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 熊磊
* @description 针对表【school_classroom(教室信息表，存储教室的基本信息)】的数据库操作Service实现
* @createDate 2025-03-03 14:15:34
*/
@Service
public class ClassroomServiceImpl extends ServiceImpl<ClassroomMapper, Classroom>
    implements ClassroomService{

    @Override
    public Long createClassroom(ClassroomCreateReqVO createReqVO) {
        Classroom bean = BeanUtils.toBean(createReqVO, Classroom.class);
        this.save(bean);
        return bean.getId();
    }

    @Override
    public void updateClassroom(ClassroomUpdateReqVO updateReqVO) {
        Classroom bean = BeanUtils.toBean(updateReqVO, Classroom.class);
        this.updateById(bean);
    }

    @Override
    public void deleteClassroom(List<Long> id) {
        this.removeByIds(id);
    }

    @Override
    public ClassroomRespVO getClassroom(Long id) {
        if (id != null) {
            Classroom classroom = this.getById(id);
            return BeanUtils.toBean(classroom, ClassroomRespVO.class);
        }
        return null;
    }

    @Override
    public List<ClassroomRespVO> getClassroomList(ClassroomListReqVO listReqVO) {
        LambdaQueryWrapper<Classroom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(listReqVO.getClassroomName()), Classroom::getClassroomName, listReqVO.getClassroomName())
                .like(Objects.nonNull(listReqVO.getBuilding()), Classroom::getBuilding, listReqVO.getBuilding())
                .eq(Objects.nonNull(listReqVO.getStatus()), Classroom::getStatus, listReqVO.getStatus());
        List<Classroom> classroomList = this.list(queryWrapper);
        if (classroomList != null) {
            return BeanUtils.toBean(classroomList, ClassroomRespVO.class);
        }
        return null;
    }

    @Override
    public PageResult<ClassroomRespVO> getClassroomListPage(ClassroomListPageReqVO listReqVO) {
        LambdaQueryWrapper<Classroom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(listReqVO.getClassroomName()), Classroom::getClassroomName, listReqVO.getClassroomName())
                .like(Objects.nonNull(listReqVO.getBuilding()), Classroom::getBuilding, listReqVO.getBuilding())
                .eq(Objects.nonNull(listReqVO.getStatus()), Classroom::getStatus, listReqVO.getStatus());
        Page<Classroom> page = this.page(new Page<>(listReqVO.getPageNo(), listReqVO.getPageSize()), queryWrapper);
        if (page != null) {
            return new PageResult<>(BeanUtils.toBean(page.getRecords(), ClassroomRespVO.class), page.getTotal());
        }
        return null;
    }
}




